package session.stateless;

import entity.ImageElementEntity;
import entity.StakeholderEntity;
import entity.TextElementEntity;
import entity.UnstructuredContentElementEntity;
import entity.UnstructuredContentEntity;
import entity.UnstructuredContentPageEntity;
import entity.UnstructuredContentPanelEntity;
import entity.VideoElementEntity;
import exception.ExistException;
import java.util.List;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 *
 * @author WenYong
 */
@Stateless
public class StakeholderUnstructuredContentSessionBean {

    @PersistenceContext
    private EntityManager entityManager;
    @EJB
    private StakeholderSessionBeanStateless stakeholderSession;

    public boolean addContent(UnstructuredContentEntity unsc, long stakeholderId) {
        boolean successful = true;
        try {
            UnstructuredContentEntity content = new UnstructuredContentEntity(unsc);
            List<UnstructuredContentPageEntity> pageList = unsc.getUnstructuredContentPage();
            for (int i = 0; i < pageList.size(); i++) {
                List<UnstructuredContentPanelEntity> panelList = pageList.get(i).getUnstructuredContentPanel();
                for (int j = 0; j < panelList.size(); j++) {
                    UnstructuredContentPanelEntity pan = panelList.get(j);
                    pan.setUnstructuredContentPageEntity(pageList.get(i));
                    List<UnstructuredContentElementEntity> elementList = pan.getUnstructuredContentElement();
                    for (int k = 0; k < elementList.size(); k++) {
                        UnstructuredContentElementEntity e = elementList.get(k);
                        e.setUnstructuredContentPanel(pan);
                        if (e.getName().contains("txt")) {
                            TextElementEntity text = (TextElementEntity) e;
                            pan.getUnstructuredContentElement().set(k, text);
                        } else if (e.getName().contains("img")) {
                            ImageElementEntity img = (ImageElementEntity) e;
                            pan.getUnstructuredContentElement().set(k, img);
                        } else if (e.getName().contains("vid")) {
                            VideoElementEntity vid = (VideoElementEntity) e;
                            pan.getUnstructuredContentElement().set(k, vid);
                        }
                    }
                }
                pageList.get(i).setUnstructuredContentPanel(panelList);
                pageList.get(i).setUnstructuredContentEntity(content);
                content.setUnstructuredContentPage(pageList);
            }
            StakeholderEntity s = stakeholderSession.getStakeholderById(stakeholderId);
            content.setStakeholder(s);
            s.setUnstructuredContent(content);
            entityManager.persist(content);
            entityManager.flush();
        } catch (Exception ex) {
            successful = false;
            ex.printStackTrace();
        }
        return successful;

    }

    public List<UnstructuredContentEntity> getContent(Long stakeholderId) throws ExistException {
        Query query = entityManager.createQuery("SELECT u FROM UnstructuredContent u WHERE u.stakeholder.id =:stakeholderId");
        query.setParameter("stakeholderId", stakeholderId);
        return query.getResultList();
    }

    public boolean editContent(List<UnstructuredContentPageEntity> unscList, UnstructuredContentPageEntity unsc, List<UnstructuredContentPanelEntity> panel, long stakeholderId) {
        boolean successful = true;
        try {
            UnstructuredContentPageEntity content = entityManager.find(UnstructuredContentPageEntity.class, unsc.getUnstructuredContentPageId());
            long tempId = content.getUnstructuredContentPageId();

            unsc.setUnstructuredContentPageId(tempId);
            List<UnstructuredContentPanelEntity> panels = viewPanels(content.getUnstructuredContentPageId());

            for (int i = 0; i < panels.size(); i++) {
                UnstructuredContentPanelEntity p = entityManager.find(UnstructuredContentPanelEntity.class, panels.get(i).getUnstructuredContentPanelId());
                if (p != null) {
                    List<TextElementEntity> TList = viewTextElements(p.getUnstructuredContentPanelId());
                    List<ImageElementEntity> IList = viewImageElements(p.getUnstructuredContentPanelId());
                    List<VideoElementEntity> VList = viewVideoElements(p.getUnstructuredContentPanelId());
                    for (int j = 0; j < TList.size(); j++) {
                        TextElementEntity e = TList.get(j);
                        if (e != null) {
                            entityManager.remove(e);
                            entityManager.flush();
                            System.out.println("removing Text elements");
                        }
                    }
                    for (int j = 0; j < IList.size(); j++) {
                        ImageElementEntity e = IList.get(j);
                        if (e != null) {
                            entityManager.remove(e);
                            entityManager.flush();
                            System.out.println("removing Image elements");
                        }
                    }
                    for (int j = 0; j < VList.size(); j++) {
                        VideoElementEntity e = VList.get(j);
                        if (e != null) {
                            entityManager.remove(e);
                            entityManager.flush();
                            System.out.println("removing Video elements");
                        }
                    }
                    entityManager.remove(p);
                    System.out.println("removing panel");
                    entityManager.flush();
                    StakeholderEntity s = stakeholderSession.getStakeholderById(stakeholderId);
                    UnstructuredContentEntity uc = s.getUnstructuredContent();
                    s.setUnstructuredContent(null);
                    entityManager.remove(uc);
                    entityManager.flush();
                    entityManager.remove(content);
                    entityManager.flush();
                }
            }
            UnstructuredContentEntity g = new UnstructuredContentEntity();
            g.setUnstructuredContentPage(unscList);
            addContent(g, stakeholderId);    
        } catch (Exception ex) {
            ex.printStackTrace();
            successful = false;
        }
        return successful;
    }

    public List<UnstructuredContentPanelEntity> viewPanels(Long pageId) {
        Query query = entityManager.createQuery("SELECT c FROM UnstructuredContentPanel c, UnstructuredContentPage p where p.unstructuredContentPageId =:pageId AND c.unstructuredContentPage = p");
        query.setParameter("pageId", pageId);
        return query.getResultList();
    }

    public List<TextElementEntity> viewTextElements(Long panelId) {
        Query query = entityManager.createQuery("SELECT c FROM TextElement c, UnstructuredContentPanel p where p.unstructuredContentPanelId =:panelId AND c.unstructuredContentPanel = p");
        query.setParameter("panelId", panelId);
        return query.getResultList();
    }

    public List<ImageElementEntity> viewImageElements(Long panelId) {
        Query query = entityManager.createQuery("SELECT c FROM ImageElement c, UnstructuredContentPanel p where p.unstructuredContentPanelId =:panelId AND c.unstructuredContentPanel = p");
        query.setParameter("panelId", panelId);
        return query.getResultList();
    }

    public List<VideoElementEntity> viewVideoElements(Long panelId) {
        Query query = entityManager.createQuery("SELECT c FROM VideoElement c, UnstructuredContentPanel p where p.unstructuredContentPanelId =:panelId AND c.unstructuredContentPanel = p");
        query.setParameter("panelId", panelId);
        return query.getResultList();
    }
}